package com.szy.web.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.szy.web.dao.NewsDAO;
import com.szy.web.model.Page;

public class newsDetailsServlet extends HttpServlet implements Servlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
	   if ( request.getSession().getAttribute("Login") != null) 
	   {
			String message = (String) request.getSession().getAttribute("Login");
			if (!message.equals("true"))
			{
				 //如果登录标志不为true则跳转回登录页面
			
				 response.sendRedirect("index.jsp");
				 return;
			}
			else
			{
			
				request.getSession().setAttribute("Login","true");
			}   
	   }
	   else
	   {
	    	response.sendRedirect("index.jsp");
	    	return;
	   }
		
		String method = request.getParameter("method");
		 if (method.equalsIgnoreCase("details")) 
		{
			int nid=Integer.parseInt(request.getParameter("nid"));
			NewsDAO newsDAOtmp;
			try {
				newsDAOtmp = new NewsDAO();
				Page detailsModel= newsDAOtmp.showNews(nid);
				request.setAttribute("detailsModel", detailsModel);
				request.getSession ( ).setAttribute ( "Login", "true" );
				request.getRequestDispatcher("newsDetails.jsp").forward(request,response);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

		}

	}
}
